package com.example.deliveryapplication

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.deliveryapplication.entity.*

class PlanItemListAdapter(
    private val context: Context,
    private val mList:List<PlanItemWithClientList>,
    private val listener:CallBack,
    private val whereFrom:Int
) : BaseRecyclerViewAdapter(context, mList) {
    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_plan_item_list, parent, false)
        return MyViewHolder(view)
    }

    override fun handlerData(holder: RecyclerView.ViewHolder, position: Int) {
        if (holder is MyViewHolder) {
            val data = mList[position]
            val details=ArrayList<Details>()
           data.list?.let { list->
               if(list.isNotEmpty()){
                   holder.clientName.text=list[0].client.name
               }
               list.forEach {
                   val item=Details()
                   item.code=it.planItem.planItemId
                   item.title="${it.model.type}-${it.model.name}"
                   item.content="${it.planItem.num}"
                   item.remark=it.planItem.remark
                   item.cu=it.planItem.cu
                   details.add(item)
               }
           }
            holder.mRecyclerView.layoutManager=LinearLayoutManager(context)
            holder.mRecyclerView.adapter=DetailsAdapter(context,details,object :DetailsAdapter.CallBack{
                override fun onItemDelete(position: Int, data: Details) {
                    listener.onItemDelete(position,data)
                }

                override fun onItemCopy(position: Int, data: Details) {

                }

            },whereFrom)
        }
    }

    inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val clientName: TextView = view.findViewById(R.id.clientName)
        val mRecyclerView: RecyclerView = view.findViewById(R.id.mRecycleView)

    }

    interface CallBack {
        fun onItemDelete(position: Int, data: Details)
    }

}